import { useCssModule } from "@vue/runtime-dom";
import { ElMessage } from "element-plus";
import * as THREE from "three";

export function transElIconName(iconName: string) {
    return iconName.replace(/[A_Z]g/, (match) => '-' + match.toLowerCase())
}

export function isValidKey(key: string | number | symbol, object: object): key is keyof typeof object {
    return key in object
}

// 动态计算content所用高度
export function getContentHeight(hasPageSize: number): number {
    // main-view的高度
    let appHeight = document.getElementsByClassName("main-view")[0].clientHeight;
    // 获取搜索栏的高度
    let searchHeight = 0;
    if (document.getElementsByClassName("search-container").length > 0) {
        searchHeight = document.getElementsByClassName("search-container")[0].clientHeight;
    }
    // 获取操作栏的高度
    let operationHeight = 0;
    if (document.getElementsByClassName("operation-container").length > 0) {
        operationHeight = document.getElementsByClassName("operation-container")[0].clientHeight + 6;
    }

    let pageSizeHeight = 0;
    if (hasPageSize) {
        pageSizeHeight = 40;
    }

    // 30为页标签高度,12为tableHeight的margin-bottom值
    let useHeight = appHeight - searchHeight - operationHeight - pageSizeHeight - 35 - 12;
    return useHeight;
}

/**
 * 格式化日期
 * @param date 
 * @param fmt 
 * @returns 
 */
export function formatDate(date, fmt) {
    date = new Date(date)
    if (typeof (fmt) === 'undefined') {
        fmt = 'yyyy-MM-dd HH:mm:ss'
    }
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'S+': date.getMilliseconds()
    }
    for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            const str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length))
        }
    }
    return fmt
}

/**
 * 返回数据套壳
 * @param res response请求返回
 * @param actions 操作字典
 * @returns 
 */
export function resHandle(res: { code: number, data: any, msg: string }, actions: any) {
    if (typeof actions[res.code] != "undefined") {
        actions[res.code](res);
        return;
    }
    if (res.code != 0) {
        if (typeof actions["default"] != "undefined") {
            actions["default"](res);
            return;
        }
        ElMessage({
            message: "数据异常",
            type: "error"
        });
    }
}

/**
 * 格式化文件大小
 * @param value 
 * @returns 
 */
export function renderSize(value) {
    if (null == value || value == '') {
        return "0 Bytes";
    }
    var unitArr = new Array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
    var index = 0;
    var srcsize = parseFloat(value);
    index = Math.floor(Math.log(srcsize) / Math.log(1024));
    var size = srcsize / Math.pow(1024, index);
    let r = size.toFixed(2);//保留的小数位数
    return r + unitArr[index];
}

/**
 * 经纬度坐标转墨卡托坐标
 * @param {经度(角度值)} longitude 
 * @param {维度(角度值)} latitude
 */
export function lon2xy(longitude, latitude) {
    var E = longitude;
    var N = latitude;
    var x = E * 20037508.34 / 180;
    var y = Math.log(Math.tan((90 + N) * Math.PI / 360)) / (Math.PI / 180);
    y = y * 20037508.34 / 180;
    return {
        x: x, //墨卡托x坐标——对应经度
        y: y, //墨卡托y坐标——对应维度
    }
}

/**
 * 3D包围盒打印
 * @param mesh 
 */
export function getBox3(mesh, isPrint=false) {
    // 包围盒计算模型对象的大小和位置
    var box3 = new THREE.Box3();
    box3.expandByObject(mesh); // 计算模型包围盒
    isPrint&&console.log('查看包围盒box3', box3);
    var size = new THREE.Vector3();
    box3.getSize(size); // 计算包围盒尺寸
    isPrint&&console.log('查看返回的包围盒尺寸', size);
    var center = new THREE.Vector3();
    box3.getCenter(center); // 计算一个层级模型对应包围盒的几何体中心坐标
    isPrint&&console.log('几何中心', center);
    return {
        box3: box3,
        size: size,
        center: center
    };
}
